<template>
  <div class="detail_background">
    <div class="detail">
      <div class="detail_item">
        <div class="detail_item_label">头像</div>
        <img class="mine_photo" :src="photo"/><!--"../../static/img/mine/touxiang.png"-->
      </div>
      <div class="detail_item">
        <span class="detail_item_label">姓名</span>
        <span class="detail_item_content">{{ realname }}</span>
      </div>
      <div class="detail_item">
        <span class="detail_item_label">账号</span>
        <span class="detail_item_content">{{ loginname }}</span>
      </div>
<!--      <div class="detail_item">-->
<!--        <span class="detail_item_label">所属城市</span>-->
<!--        <span class="detail_item_content">{{ region }}</span>-->
<!--      </div>-->
    </div>
    <div class="loginOut" @tap="loginOut">
      退出登录
    </div>
  </div>
</template>

<script>
  import LoginManager from "@/utils/LoginManager";
  import wxPromisify from "@/libs/wxPromisify";

  export default {
    name: "mineDetail",
    data() {
      return {
        realname:'',
        loginname: '',
        photo:'',
        // region: '',
      }
    },

    // onLoad() {
    //   this.fetchLoginInfo();
    //   if(LoginManager.getToken()){
    //     let params = {
    //       token: LoginManager.getToken(),
    //     }
    //     this.$Api.adminRedisInfo(params)
    //         .then((res) => {
    //           console.log('res====', res);
    //           this.realname = res.realname;//员工姓名;
    //           this.loginname=res.loginname;//员工账号
    //         });
    //   }
    // },
    onShow() {
      this.fetchLoginInfo();
      if(LoginManager.getToken()){
        let params = {
          token: LoginManager.getToken(),
        }
        this.$Api.adminRedisInfo(params)
            .then((res) => {
              console.log('res====', res);
              // this.realname = res.realname;//员工姓名;
              // this.loginname=res.loginname;//员工账号
              if(res!=undefined && res.code=='99'){//说明token已失效，需要重新登录
                this.stationList=[];
                this.realname ='请登录';
              }
              if(res.returnCode=='0'){//获取成功
                let data=res.resultData;
                this.realname = data.realname;//员工姓名;
                this.photo=data.photo!=null && data.photo!=""?data.photo:'../../static/img/mine/touxiang.png';//员工头像
                this.loginname=data.loginname;//员工账号
              }
            });
      }
    },
    methods: {
      async fetchLoginInfo() {
        if(this.$LoginManager.getLoginType()==="admin"){//业务员
          let isLogin = this.$LoginManager.checkLoginStatus();
          if (!isLogin) {
            await wxPromisify.navigateTo({
              url: '/pages/login/adminIndex'//业务员登录
            })
          }else{
            let {returnCode, resultData} = await LoginManager.appletsCheckToken(LoginManager.getToken());//校验token是否过期
            if(returnCode!="0"){//token已过期 不存在
              let {returnCode, resultData} = await this.$LoginManager.CheckLoginByCode();//以前登陆过重新生成token
              if(returnCode!="0"){//returnCode等于0代表自动登录，不为0代表该openid在表中未查到，打开登录页面
                await wxPromisify.navigateTo({
                  url: '/pages/login/adminIndex'//业务员登录
                })
              }
            }
          }
        }else if(!this.$LoginManager.getLoginType()){//以前没登陆过跳转到登录页
          await wxPromisify.navigateTo({
            url: '/pages/login/adminIndex'
          })
        }
      },
      loginOut() {
        let params = {
          token: LoginManager.getToken(),
        }
        this.$Api.loginOut(params)
          .then((res) => {
            console.log('res====', res);
            console.log('res.returnCode====', res.returnCode);
            if (res.returnCode === "0") {
              wx.showToast(
              {
                title: '登出成功',
                  icon: 'none',
                duration: 2000
              });
              uni.clearStorage();
              wxPromisify.switchTab({
                url: '/pages/mine/index'
              })
              // uni.removeStorageSync('login_token');
              // uni.removeStorageSync('login_type');
            }
          })
      }
    }
  }
</script>

<style scoped lang="scss">
  .detail_background {
    min-height: 100VH;
    background: #F5F5F5;
  }

  .detail {
    width: 750px;
    height: 346px;
    background: #FFFFFF;
    margin-top: 24px;
    margin-bottom: 24px;
    //display: flex;
    //flex-direction: row;
    //margin-left: 30px;
    //margin-right: 30px;
    //padding-top: 40px;
    //padding-bottom: 40px;
    //border-bottom: 1px solid #E6E6E6;
  }

  .mine_photo {
    width: 72px;
    height: 72px;
    text-align: right;
    border-radius: 50%;
  }

  .detail_item {
    margin-left: 60px;
    margin-right: 60px;
    height: 114px;
    //position: relative;
    border-bottom: 1px solid #E6E6E6;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  .detail_item:last-child {
    border-bottom: 0;
  }

  .detail_item_label {
    //display: inline-block;
    font-size: 32px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #333333;
    line-height: 32px;
  }

  .detail_item_content {
    //margin-left: 18px;
    color: #333333;
    text-align: right;
  }

  .loginOut {
    height: 80px;
    background: #FFFFFF;
    font-size: 32px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #D9261C;
    line-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>